package com.lottery.shadowbd.utils;

import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/21 0021.
 */
public class SharedPreUtil {

    private static SharedPreUtil sharedPreferencesUtil;
    private static Context mContext;
    private SharedPreferences sharedPreferences;
    private static int mode = mContext.MODE_PRIVATE;
    private SharedPreferences.Editor editor;
    private static String sharedPreferenceName = "LotterySBDSp";

    public SharedPreUtil() {
    }

    public static SharedPreUtil getInstance(Context mContext) {
        SharedPreUtil.mContext = mContext;
        if(sharedPreferencesUtil == null){
            sharedPreferencesUtil =  new SharedPreUtil();
        }
        return sharedPreferencesUtil;
    }

    public static SharedPreUtil getInstance(Context context, String sharedPreferenceName, int mode){
        SharedPreUtil.mContext = context;
        SharedPreUtil.sharedPreferenceName = sharedPreferenceName;
        SharedPreUtil.mode = mode;
        if(sharedPreferencesUtil == null){
            sharedPreferencesUtil =  new SharedPreUtil();
        }
        return sharedPreferencesUtil;
    }

    private void createSharedPreferences(){
        if(sharedPreferences == null) {
            sharedPreferences = mContext.getSharedPreferences(sharedPreferenceName, mode);
        }
    }

    public SharedPreferences getSharedPreferences(){
        createSharedPreferences();
        return sharedPreferences;
    }

    /**
     * 获取 SharedPreferences boolean值
     * @param key 键
     * @param defValue 默认值
     * @return
     */
    public boolean getBoolean(String key, boolean defValue){
        createSharedPreferences();
        return sharedPreferences.getBoolean(key, defValue);
    }

    /**
     * 获取 SharedPreferences String值
     * @param key 键
     * @param defValue 默认值
     * @return
     */
    public String getString(String key, String defValue){
        createSharedPreferences();
        return sharedPreferences.getString(key, defValue);
    }


    public void putString(String key, String value){
        createSharedPreferences();
        editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void putInt(String key, int value){
        createSharedPreferences();
        editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 获取 SharedPreferences String值
     * @param key 键
     * @param defValue 默认值
     * @return
     */
    public Integer getInt(String key, int defValue){
        createSharedPreferences();
        return sharedPreferences.getInt(key, defValue);
    }

    public void putBoolean(String key, boolean value){
        createSharedPreferences();
        editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 删除key
     * @param key
     */
    public void removeKey(String key){
        createSharedPreferences();
        editor = sharedPreferences.edit();
        editor.remove(key);
        editor.commit();
    }

    public void saveInfo(Context context, String key, List<Map<String, String>> datas) {
        JSONArray mJsonArray = new JSONArray();
        for (int i = 0; i < datas.size(); i++) {
            Map<String, String> itemMap = datas.get(i);
            Iterator<Map.Entry<String, String>> iterator = itemMap.entrySet().iterator();

            JSONObject object = new JSONObject();

            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                try {
                    object.put(entry.getKey(), entry.getValue());
                } catch (JSONException e) {

                }
            }
            mJsonArray.put(object);
        }

        SharedPreferences sp = context.getSharedPreferences("finals", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, mJsonArray.toString());
        editor.commit();
    }

    public List<Map<String, String>> getInfo(Context context, String key) {
        List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
        SharedPreferences sp = context.getSharedPreferences("finals", Context.MODE_PRIVATE);
        String result = sp.getString(key, "");
        try {
            JSONArray array = new JSONArray(result);
            for (int i = 0; i < array.length(); i++) {
                JSONObject itemObject = array.getJSONObject(i);
                Map<String, String> itemMap = new HashMap<String, String>();
                JSONArray names = itemObject.names();
                if (names != null) {
                    for (int j = 0; j < names.length(); j++) {
                        String name = names.getString(j);
                        String value = itemObject.getString(name);
                        itemMap.put(name, value);
                    }
                }
                datas.add(itemMap);
            }
        } catch (JSONException e) {

        }

        return datas;
    }
}
